package com.zxj.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zxj.shop.admin.entity.Permission;
import com.zxj.shop.admin.mapper.PermissionMapper;
import com.zxj.shop.admin.service.PermissionService;
import com.zxj.shop.admin.shiro.vo.PermissionRespNode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionMapper permissionMapper;

	@Override
	public List<Permission> getUserPermission(String roleId) {
		return permissionMapper.getUserPermission(roleId);
	}

	@Override
	public List<Permission> list() {
		return permissionMapper.selectList(Wrappers.emptyWrapper());
	}

	@Override
	public List<PermissionRespNode> selectAllMenuByTree(String permissionId) {
		List<Permission> list = list();
		if (!CollectionUtils.isEmpty(list) && !StringUtils.isEmpty(permissionId)) {
			for (Permission sysPermission : list) {
				if (sysPermission.getId().equals(permissionId)) {
					list.remove(sysPermission);
					break;
				}
			}
		}
		List<PermissionRespNode> result = new ArrayList<>();
		//新增顶级目录是为了方便添加一级目录
		PermissionRespNode respNode = new PermissionRespNode();
		respNode.setId("1");
		respNode.setTitle("默认顶级菜单");
		respNode.setSpread(true);
		respNode.setChildren(getTree(list, true));
		result.add(respNode);
		return result;
	}

	/**
	 * 递归获取菜单树
	 */
	private List<PermissionRespNode> getTree(List<Permission> all, boolean type) {
		List<PermissionRespNode> list = new ArrayList<>();

		for (Permission sysPermission : all) {
			if (1 == sysPermission.getPid()) {
				PermissionRespNode permissionRespNode = new PermissionRespNode();
				permissionRespNode.setId(sysPermission.getId().toString());
				permissionRespNode.setTitle(sysPermission.getName());
				permissionRespNode.setPid(sysPermission.getPid().toString());
				permissionRespNode.setOrderNum(sysPermission.getSort());

				if (type) {
					permissionRespNode.setChildren(getChildExcBtn(sysPermission.getId(), all));
				} else {
					permissionRespNode.setChildren(getChildAll(sysPermission.getId(), all));
				}
				list.add(permissionRespNode);
			}
		}
		return list;
	}

	/**
	 * 递归遍历所有
	 */
	private List<PermissionRespNode> getChildAll(Integer id, List<Permission> all) {

		List<PermissionRespNode> list = new ArrayList<>();
		for (Permission sysPermission : all) {
			if (sysPermission.getPid() == id) {
				PermissionRespNode permissionRespNode = new PermissionRespNode();
				permissionRespNode.setId(sysPermission.getId().toString());
				permissionRespNode.setTitle(sysPermission.getName());
				permissionRespNode.setPid(sysPermission.getPid().toString());
				permissionRespNode.setOrderNum(sysPermission.getSort());
				permissionRespNode.setChildren(getChildAll(sysPermission.getId(), all));
				list.add(permissionRespNode);
			}
		}
		return list;
	}

	/**
	 * 只递归获取目录和菜单
	 */
	private List<PermissionRespNode> getChildExcBtn(Integer id, List<Permission> all) {

		List<PermissionRespNode> list = new ArrayList<>();
		for (Permission sysPermission : all) {
			if (sysPermission.getPid().intValue() == id && sysPermission.getCategory() != 3) {
				PermissionRespNode permissionRespNode = new PermissionRespNode();
				permissionRespNode.setId(sysPermission.getId().toString());
				permissionRespNode.setTitle(sysPermission.getName());
				permissionRespNode.setPid(sysPermission.getPid().toString());
				permissionRespNode.setOrderNum(sysPermission.getSort());
				permissionRespNode.setChildren(getChildExcBtn(sysPermission.getId(), all));
				list.add(permissionRespNode);
			}
		}
		return list;
	}


}
